package evq.codec.ui;

import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import evq.codec.core.EvqApplication;

/**
 * @author Gutemberg Rodrigues
 */
public class InputLBGFrameDialog extends javax.swing.JDialog {

    private JFileChooser jFileChooser;
    private Vector<String> lista;
    private EvqApplication app;

    public InputLBGFrameDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        lista = new Vector<String>();
        app = EvqApplication.getInstance();
        setLocationRelativeTo(null);
    }

    @SuppressWarnings("unchecked")

    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        labelTrainingSet = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTrainingSet = new javax.swing.JList();
        bntAdd = new javax.swing.JButton();
        bntRemove = new javax.swing.JButton();
        bntGenerateDictionary = new javax.swing.JButton();
        bntClean = new javax.swing.JButton();
        labelDimension = new javax.swing.JLabel();
        txtDimension = new javax.swing.JTextField();
        txtSize = new javax.swing.JTextField();
        labelSize = new javax.swing.JLabel();
        txtArquiveDestination = new javax.swing.JTextField();
        labelArquiveDestination = new javax.swing.JLabel();
        bntSearch = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        labelTrainingSet.setText("Conjunto de Treino:");

        jScrollPane1.setViewportView(listTrainingSet);

        bntAdd.setText("Adicionar");
        bntAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAddActionPerformed(evt);
            }
        });

        bntRemove.setText("Remover");
        bntRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntRemoveActionPerformed(evt);
            }
        });

        bntGenerateDictionary.setText("Gerar Dicionário");
        bntGenerateDictionary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntGenerateDictionaryActionPerformed(evt);
            }
        });

        bntClean.setText("Limpar Campos");
        bntClean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCleanActionPerformed(evt);
            }
        });

        labelDimension.setText("Dimensão:");

        txtDimension.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDimensionActionPerformed(evt);
            }
        });

        txtSize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSizeActionPerformed(evt);
            }
        });

        labelSize.setText("Tamanho:");

        txtArquiveDestination.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtArquiveDestinationActionPerformed(evt);
            }
        });

        labelArquiveDestination.setText("Arquivo Destino:");

        bntSearch.setText("...");
        bntSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTrainingSet)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(bntAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bntRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(45, 45, 45)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDimension, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelDimension)
                            .addComponent(txtSize, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelSize)
                            .addComponent(labelArquiveDestination)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(bntClean)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(bntGenerateDictionary, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(txtArquiveDestination, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(bntSearch))))))
                .addGap(16, 16, 16))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTrainingSet)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(labelDimension)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(labelSize)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(labelArquiveDestination)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtArquiveDestination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bntSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bntGenerateDictionary)
                            .addComponent(bntClean)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bntRemove)
                            .addComponent(bntAdd))))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }

    private void bntAddActionPerformed(java.awt.event.ActionEvent evt) {
       lista.add(iniFileChooser());
       listTrainingSet.setListData(lista);
    }

    private void bntGenerateDictionaryActionPerformed(java.awt.event.ActionEvent evt) {
    	try{
	    	if(lista.isEmpty() || (txtSize.getText().equals("") || txtSize == null) || (txtDimension.getText().equals("") || txtDimension == null) || (txtArquiveDestination.getText().equals("") || txtArquiveDestination == null)){
	    		JOptionPane.showMessageDialog(this, "Todos os Campos Precisam ser Informados!", "Atenção", JOptionPane.ERROR_MESSAGE);
	    	}else{
	    		app.createDictionary(lista, Integer.parseInt(txtSize.getText()), Integer.parseInt(txtDimension.getText()), txtArquiveDestination.getText());
	    		JOptionPane.showMessageDialog(this, "Processo Finalizado!", "Fim", JOptionPane.INFORMATION_MESSAGE);
	    	}
    	}catch(Exception e){
    		JOptionPane.showMessageDialog(this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
    	}
    	
    }

    private void txtDimensionActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void txtSizeActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void txtArquiveDestinationActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void bntRemoveActionPerformed(java.awt.event.ActionEvent evt) {
        lista.remove(listTrainingSet.getSelectedIndex());
        listTrainingSet.setListData(lista);
    }

    private void bntSearchActionPerformed(java.awt.event.ActionEvent evt) {
        txtArquiveDestination.setText(iniFileChooser());
    }

    private void bntCleanActionPerformed(java.awt.event.ActionEvent evt) {
        txtArquiveDestination.setText(null);
        txtDimension.setText(null);
        txtSize.setText(null);
    }

   private String iniFileChooser(){
        jFileChooser = new JFileChooser();
        JFrame frame = new JFrame();
        jFileChooser.setVisible(true);
        jFileChooser.showOpenDialog(frame);
        String path = "";
        if(jFileChooser.getSelectedFile()!=null){
            path = jFileChooser.getSelectedFile().getAbsolutePath();
        }
        return path;
   }

   public void openInputLBGFrame(){
       InputLBGFrameDialog inputFrame = new InputLBGFrameDialog(new InitialFrame(), true);
       inputFrame.setVisible(true);
  }

    private javax.swing.JButton bntAdd;
    private javax.swing.JButton bntClean;
    private javax.swing.JButton bntGenerateDictionary;
    private javax.swing.JButton bntRemove;
    private javax.swing.JButton bntSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelArquiveDestination;
    private javax.swing.JLabel labelDimension;
    private javax.swing.JLabel labelSize;
    private javax.swing.JLabel labelTrainingSet;
    private javax.swing.JList listTrainingSet;
    private javax.swing.JTextField txtArquiveDestination;
    private javax.swing.JTextField txtDimension;
    private javax.swing.JTextField txtSize;

}
